package com.controller;

import com.bean.User;
import com.service.UserService;
import com.util.PageAssistant;
import com.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 登录
     * @param session
     * @param user
     * @return
     */
    @RequestMapping("login")
    public String login(HttpSession session, User user){
        System.out.println(">>>>> user login");
        System.out.println("user : "+user);
        System.out.println("------------------------------");

        List<User> loginInfo = userService.find(user);

        if(loginInfo.size() == 1){
            if(loginInfo.get(0).getState() != 0){
                session.setAttribute("loginInfo",loginInfo.get(0));
                return "redirect:/main";
            }else{
                session.setAttribute("msg","账号已注销");
                return "redirect:/login";
            }
        }

        session.setAttribute("msg","用户名或密码错误");
        return "redirect:/login";
    }

    /**
     * 退出
     * @param session
     * @return
     */
    @RequestMapping("logoff")
    public String logoff(HttpSession session){
        System.out.println(">>>> user logoff");
        session.invalidate();
        return "redirect:/login";
    }
    /**
     * 查询所有员工信息
     * @param session
     * @param model
     * @param paUser
     * @param user
     * @return
     */
    @RequestMapping("query")
    public String query(HttpSession session, Model model, PageAssistant<User> paUser, User user){
        System.out.println(">>>>>>>>> user query");
        System.out.println(user);
        System.out.println("-------------------------");
        if(session.getAttribute("msg") != null){
            model.addAttribute("msg",session.getAttribute("msg"));
            session.removeAttribute("msg");
        }

        Map<String,Object> map = new HashMap<String,Object>();

        map.put("start",paUser.getStart());
        map.put("size",paUser.getSize());

        paUser.setCount(userService.count(map));
        paUser.setItems(userService.query(map));

        model.addAttribute("paUser",paUser);

        return "page/user/userList";
    }
    /**
     * 个人信息查询页
     * @param model
     * @param user
     * @return
     */
    @RequestMapping("unique")
    public String unique(Model model, User user){
        System.out.println(">>>>>> user unique");
        System.out.println("user:"+user);
        System.out.println("------------------");
        List<User> userList = userService.find(user);
        if(userList.size() == 1){
            model.addAttribute("userList",userList.get(0));
            return "page/user/unique";
        }
        return null;
    }



    /**
     * 添加用户信息
     * @return
     */
    @RequestMapping("add")
    public String add(HttpSession session,Model model){

        return "page/user/add";
    }

    /**
     * 查看员工信息
     * @param model
     * @param user
     * @return
     */
    @RequestMapping("see")
    public String userSee(Model model, User user){
        System.out.println(">>>>>> user see");
        System.out.println("user:"+user);
        System.out.println("------------------");
        List<User> userSee = userService.find(user);
        if(userSee.size() == 1){
            model.addAttribute("userSee",userSee.get(0));
            return "page/user/userSee";
        }
        return null;
    }

    /**
     * 删除
     * @param session
     * @param model
     * @param user
     * @return
     */
    @RequestMapping("delete")
    public String delete(HttpSession session, Model model, User user){
        System.out.println(">>>>>> user delete");
        System.out.println("user:"+user);
        System.out.println("------------------");
        int remove = userService.remove(user);
        if(remove == 1){
            session.setAttribute("msg","删除成功");
        }else{
            session.setAttribute("msg","删除失败");
        }
        return "redirect:/user/query";
    }

    /**
     * 添加
     * @return
     */
    @RequestMapping("save")
    public String save(HttpSession session, Model model, UserVo user){
        System.out.println(">>>>>> user save");
        System.out.println("user:"+user);
        System.out.println("------------------");
        Map<String,Object> map = new HashMap<>();
        List<User> queryList = userService.query(map);

        for(int i = 0;i<queryList.size();i++){
            if(user.getUserName().equals(queryList.get(i).getUserName())){
                model.addAttribute("msg","账号已存在");
                return "page/user/add";
            }
        }
        user.setState("1");
        int save = userService.save(user.parse());
        System.out.println("save"+save);
        if(save == 1){
            session.setAttribute("msg","添加成功");
            return "redirect:/user/query";
        }
        return null;
    }
   @RequestMapping("update")
    public String update(HttpSession session, Model model, UserVo user){
       System.out.println(">>>>>> user update");
       System.out.println("user:"+user);
       System.out.println("------------------");

       int modify = userService.modify(user.parse());

       if(modify == 1){
           session.setAttribute("msg","修改成功");
       }else {
           session.setAttribute("msg","修改失败");
       }
       return "redirect:/user/see?id="+user.getId();
    }

}
